//
//  GuidePageViewController.m
//  OnlineEducation
//
//  Created by dhcui on 16/5/5.
//  Copyright © 2016年 dhcui. All rights reserved.
//

#import "GuidePageViewController.h"
#import "JrdRootViewController.h"
#import "JrdAppDelegate.h"
#import "JrdLoginViewController.h"
#import "JrdContactUsViewController.h"
#import "CKHttpCommunicate.h"
#import "JrdCommon.h"
#import "JrdDatamanager.h"
#import "JrdRegisterViewController.h"
@interface GuidePageViewController ()<UIScrollViewDelegate,UIAlertViewDelegate>
@property(nonatomic, retain)UIScrollView *scrollView;
@property(nonatomic, retain)UIImageView *imageViewPic;
@property(nonatomic, retain)NSMutableArray *picArr;
@property(nonatomic, retain)UIPageControl *pageControl;

@property(nonatomic, retain)UIButton *buttonNext;
@property(nonatomic, retain)UIButton *buttonLogin;

@property(nonatomic, retain)NSString *pageType;

@end

@implementation GuidePageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [[NSUserDefaults standardUserDefaults] setObject:@(100) forKey:kCheckValue];

    self.pageType= @"guide";
    
    self.picArr = @[[UIImage imageNamed:@"guidePage_01"], [UIImage imageNamed:@"guidePage_02"], [UIImage imageNamed:@"guidePage_03"], [UIImage imageNamed:@"guidePage_04"]].mutableCopy;
    [self getCheckValueFromServer];

    /* 程序是否为第一次启动 */
    //set to YES, just for test
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"isLogin"] == NO) {
        
        //设置值， 初始化wifi下下载为true
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"vDownloadUnderWifi"];
        [self createScrollView];
        [self createPageControl];
    } else {
        //just for test
        JrdAppDelegate *tempAppDelegate = [UIApplication sharedApplication].delegate;
        JrdRootViewController* menuView = [[JrdRootViewController alloc] init];
        tempAppDelegate.window.rootViewController = menuView;//tempAppDelegate.tb;
        [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"isLogin"];
    }
}

- (void)getCheckValueFromServer{
    [CKHttpCommunicate createRequest:HTTP_GET_CHECKVALUE WithParam:nil withMethod:GET success:^(id result) {
        if(result){
            int specialFlag = [[result valueForKey:@"specialflag"] intValue];
            if(JRD_CHECK_VALUE ==  specialFlag){
                [[NSUserDefaults standardUserDefaults] setObject:@(JRD_CHECK_VALUE) forKey:kCheckValue];
            }else{
                [[NSUserDefaults standardUserDefaults] setObject:@(0) forKey:kCheckValue];
            }
            
            if(JRD_CHECK_VALUE == [[JrdDatamanager manager] getCheckValue] || 100 == [[JrdDatamanager manager] getCheckValue]){
                [self.buttonLogin setTitle:NSLocalizedString(@"jrd_gointo_study", nil) forState:UIControlStateNormal];
                [self.buttonLogin addTarget:self action:@selector(handleGoinApp) forControlEvents:UIControlEventTouchUpInside];
                
            }else{
                [self.buttonLogin setTitle:NSLocalizedString(@"Jrd_Login", nil) forState:UIControlStateNormal];
                [self.buttonLogin addTarget:self action:@selector(handleLoginInApp) forControlEvents:UIControlEventTouchUpInside];
            }

            
        }
    }failure:^(NSError *erro) {
        [[NSUserDefaults standardUserDefaults] setObject:@(100) forKey:kCheckValue];
    } showHUD:nil];
    
}

- (void)createPageControl {
    
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 40)/ 2, self.view.frame.size.height - 50, 40, 20)];
    [self.view addSubview:self.pageControl];
    self.pageControl.numberOfPages = 4;
    self.pageControl.currentPageIndicatorTintColor = [UIColor colorWithRed:0.278 green:0.312 blue:0.541 alpha:1.000];
    self.pageControl.pageIndicatorTintColor = [UIColor colorWithWhite:0.702 alpha:1.000];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)createScrollView {
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [self.view addSubview:self.scrollView];
    self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 4, self.view.frame.size.height);
    self.scrollView.pagingEnabled = YES;
    self.scrollView.bounces = NO;
    self.scrollView.delegate = self;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    
    /* 创建imageView */
    for (int i = 1; i < 5; i++) {
        
        self.imageViewPic = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width*(i - 1), 0,self.view.frame.size.width, self.view.frame.size.height)];
        self.imageViewPic.image = [self.picArr objectAtIndex:i - 1];
        [self.scrollView addSubview:self.imageViewPic];
        self.imageViewPic.tag = i * 100;
        if (i == 4) {
            /*下一页按钮 */
            self.buttonNext = [UIButton buttonWithType:UIButtonTypeCustom];
            self.buttonNext.frame = CGRectMake((self.view.frame.size.width - 240) / 2 + self.view.frame.size.width * 3 , self.view.frame.size.height - 120, 240, 40);
            [self.buttonNext setTitle:NSLocalizedString(@"Jrd_Contact_US", nil) forState:UIControlStateNormal];
            [self.buttonNext setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [self.buttonNext addTarget:self action:@selector(handleGoInApp) forControlEvents:UIControlEventTouchUpInside];
            [self.scrollView addSubview:self.buttonNext];
            self.buttonNext.layer.borderWidth = 1;
            self.buttonNext.layer.borderColor = [UIColor colorWithRed:0.656 green:0.688 blue:0.869 alpha:1.000].CGColor;
            self.buttonNext.layer.cornerRadius = 5;
            
            
            self.buttonLogin = [UIButton buttonWithType:UIButtonTypeCustom];
            [self.buttonLogin setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

            //self.buttonLogin.frame = CGRectMake((self.view.frame.size.width - 280) / 2 + 160 + self.view.frame.size.width * 3 , self.view.frame.size.height - 120, 120, 40);
            self.buttonLogin.frame = CGRectMake((self.view.frame.size.width - 240) / 2 + self.view.frame.size.width * 3 , self.view.frame.size.height - 120 - 40 -10, 240, 40);
            [self.scrollView addSubview:self.buttonLogin];
            self.buttonLogin.layer.borderWidth = 1;
            self.buttonLogin.layer.borderColor = [UIColor colorWithRed:0.656 green:0.688 blue:0.869 alpha:1.000].CGColor;
            self.buttonLogin.layer.cornerRadius = 5;

            /* 登陆按钮 */
            //int checkvalue = [[JrdDatamanager manager] getCheckValue];
            /*if(JRD_CHECK_VALUE == [[JrdDatamanager manager] getCheckValue] || 100 == [[JrdDatamanager manager] getCheckValue]){
                [self.buttonLogin setTitle:NSLocalizedString(@"jrd_gointo_study", nil) forState:UIControlStateNormal];
                [self.buttonLogin addTarget:self action:@selector(handleGoinApp) forControlEvents:UIControlEventTouchUpInside];

            }else{*/
                [self.buttonLogin setTitle:NSLocalizedString(@"Jrd_Login", nil) forState:UIControlStateNormal];
                [self.buttonLogin addTarget:self action:@selector(handleLoginInApp) forControlEvents:UIControlEventTouchUpInside];
            //}
        }
        
    }
}


- (void)handleRegister{
    //go to register
    JrdRegisterViewController* registerViewController = [[JrdRegisterViewController alloc] init];
    [self presentViewController:registerViewController animated:YES completion:^{
    }];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if(1 == buttonIndex){
        [self getCheckValueFromServer];
    }
}

-(int)getRandomNumber:(int)from to:(int)to

{
    
    return (int)(from + (arc4random() % (to-from + 1)));
    
}
- (void)handleGoinApp{
    if(0 == [[JrdDatamanager manager] getCheckValue]){
        return;
    }
    if(100 == [[JrdDatamanager manager] getCheckValue]){
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"jrd_network_error", nil) message:NSLocalizedString(@"jrd_network_error_des", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"jrd_cancel", nil) otherButtonTitles:NSLocalizedString(@"jrd_retry", nil), nil];
        [alert show];

    }else{
        //Go into app directly
        JrdUserModel* user = [[JrdUserModel alloc] init];
        user.isLogin =  YES;
        //获取随机数作为name
        //int randomName = [self getRandomNumber:1000 to:10000];
        user.userName = NSLocalizedString(@"v2_me", nil);//[NSString stringWithFormat:@"%d",randomName];
        user.userId =  [NSString stringWithFormat:@"%@",@(4)];//[result valueForKey:@"id"];
        [JrdDatamanager manager].user = user;
        [UIView animateWithDuration:1 animations:^{
            
            /* 改变透明度, 并重新指定跟视图 */
            self.view.alpha = 0.1;
        } completion:^(BOOL finished) {
            /* 重新指定跟视图 */
            JrdAppDelegate *tempAppDelegate = [UIApplication sharedApplication].delegate;
            JrdRootViewController* rootView = [[JrdRootViewController alloc] init];
            tempAppDelegate.window.rootViewController = rootView;
        }];
        
        [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"isLogin"];

    }
}

- (void)goIntoApp{
    //Go into app directly
    JrdUserModel* user = [[JrdUserModel alloc] init];
    user.isLogin =  YES;
    user.userId =  [NSString stringWithFormat:@"%@",@(4)];//[result valueForKey:@"id"];
    [JrdDatamanager manager].user = user;
    JrdAppDelegate *tempAppDelegate = [UIApplication sharedApplication].delegate;
    JrdRootViewController* rootView = [[JrdRootViewController alloc] init];
    tempAppDelegate.window.rootViewController = rootView;

}


- (void)handleLoginInApp
{
    JrdLoginViewController *loginViewController = [[JrdLoginViewController alloc] init];
    loginViewController.isNeedBack = YES;
    [self presentViewController:loginViewController animated:YES completion:^{
    }];

}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    self.pageControl.currentPage = self.scrollView.contentOffset.x / self.view.frame.size.width;
}

#pragma button click
/*点此进入*/
- (void)handleGoInApp {
    JrdContactUsViewController* view = [[JrdContactUsViewController alloc] init];
    [self presentViewController:view animated:YES completion:^{
    
    }];
}


@end
